package com.newear.marathon.config;

import com.newear.marathon.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author MicroBin
 * @description：security配置类
 * @date 2021/4/21 8:02 下午
 */
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter
{

    // 配置文件参数
    @Autowired
    private SecurityProperties securityProperties;
    @Autowired
    private UserDetailsService customUserDetailService;

    @Bean("encoder")
    public BCryptPasswordEncoder bCryptPasswordEncoder()
    {
        return new BCryptPasswordEncoder();
    }

    /**
     * password 密码模式要使用此认证管理器
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception
    {
        return super.authenticationManagerBean();
    }

    /**
     * 数据库的方式读取用户信息
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth.userDetailsService(customUserDetailService).passwordEncoder(bCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http.authorizeRequests()
                // 首页所有人都可以访问
                .antMatchers(securityProperties.getAuthentication().getLoginPage(), securityProperties.getAuthentication().getImageCodeUrl(),
                             securityProperties.getAuthentication().getLoginProcessingUrl()).permitAll()
                //其他要认证后才可以访问，如 /member
                .anyRequest().authenticated().and().formLogin().usernameParameter("username").passwordParameter("password")
                .loginPage(securityProperties.getAuthentication().getLoginPage()).and().headers().frameOptions().disable()//防止iframe
                .and().csrf().disable();//防止跨域伪造
        //配置无权限访问页面
        //http.exceptionHandling().accessDeniedPage("/uanuth.html");
    }

    /**
     * 一般是针对静态资源放行
     *
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web)
    {
        web.ignoring().antMatchers(securityProperties.getAuthentication().getStaticPaths());
    }
}
